/*
Copyright 2017 yangchong211（github.com/yangchong211）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.yc.video.ui.view;

import ohos.aafwk.ability.Ability;
import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;
import ohos.multimodalinput.event.TouchEvent;

import com.yc.video.ResourceTable;
import com.yc.video.bridge.ControlWrapper;
import com.yc.video.config.ConstantKeys;
import com.yc.video.tool.PlayerUtils;

/**
 * <pre>
 *     @author yangchong
 *     blog  : https://github.com/yangchong211
 *     time  : 2017/11/9
 *     desc  : 出错提示界面
 *     revise:
 * </pre>
 */
public class CustomErrorView extends DirectionalLayout implements InterControlView, Component.ClickedListener, Component.TouchEventListener {
    private Context mContext;
    private Text mTvMessage;
    private Text mTvRetry;
    private Image mIvStopFullscreen;

    private ControlWrapper mControlWrapper;

    public CustomErrorView(Context context) {
        super(context);
        init(context);
    }

    public CustomErrorView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context);
    }

    public CustomErrorView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        setVisibility(HIDE);
        Component component = LayoutScatter.getInstance(mContext).parse(
            ResourceTable.Layout_custom_video_player_error, this, true);
        initFindViewById(component);
        initListener();
        setClickable(true);
    }

    private void initFindViewById(Component view) {
        mTvMessage = (Text) view.findComponentById(ResourceTable.Id_tv_message);
        mTvRetry = (Text) view.findComponentById(ResourceTable.Id_tv_retry);
        mIvStopFullscreen = (Image) view.findComponentById(ResourceTable.Id_iv_stop_fullscreen);
    }

    private void initListener() {
        mTvRetry.setClickedListener(this::onClick);
        mIvStopFullscreen.setClickedListener(this::onClick);
    }

    @Override
    public void attach(ControlWrapper controlWrapper) {
        mControlWrapper = controlWrapper;
    }

    @Override
    public Component getView() {
        return this;
    }

    @Override
    public void onVisibilityChanged(boolean isVisible, Animator anim) {
    }

    @Override
    public void onPlayStateChanged(int playState) {
        if (playState == ConstantKeys.CurrentState.STATE_ERROR) {
            moveChildToFront(this);
            setVisibility(VISIBLE);
            mIvStopFullscreen.setVisibility(mControlWrapper.isFullScreen() ? VISIBLE : HIDE);
            mTvMessage.setText("视频播放异常");
        }
        if (playState == ConstantKeys.CurrentState.STATE_NETWORK_ERROR) {
            moveChildToFront(this);
            setVisibility(VISIBLE);
            mIvStopFullscreen.setVisibility(mControlWrapper.isFullScreen() ? VISIBLE : HIDE);
            mTvMessage.setText("无网络，请检查网络设置");
        }
        if (playState == ConstantKeys.CurrentState.STATE_PARSE_ERROR) {
            moveChildToFront(this);
            setVisibility(VISIBLE);
            mIvStopFullscreen.setVisibility(mControlWrapper.isFullScreen() ? VISIBLE : HIDE);
            mTvMessage.setText("视频加载错误");
        } else if (playState == ConstantKeys.CurrentState.STATE_IDLE) {
            setVisibility(HIDE);
        } else if (playState == ConstantKeys.CurrentState.STATE_ONCE_LIVE) {
            setVisibility(HIDE);
        }
    }

    @Override
    public void onPlayerStateChanged(int playerState) {
    }

    @Override
    public void setProgress(int duration, int position) {
    }

    @Override
    public void onLockStateChanged(boolean isLock) {
    }

    @Override
    public void onClick(Component component) {
        if (component == mTvRetry) {
            setVisibility(HIDE);
            mControlWrapper.replay(false);
        } else if (component == mIvStopFullscreen) {
            // 点击返回键
            if (mControlWrapper.isFullScreen()) {
                Ability activity = PlayerUtils.scanForActivity(mContext);
                if (activity != null && !mControlWrapper.isFullScreen()) {
                    activity.setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT);
                    mControlWrapper.stopFullScreen();
                }
            }
        }
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                /*  mDownX = touchEvent.getPointerPosition(touchEvent.getIndex()).getX();
                mDownY = touchEvent.getPointerPosition(touchEvent.getIndex()).getY();
                getParent().requestDisallowInterceptTouchEvent(true);
                break; */
            case TouchEvent.POINT_MOVE:
                /*  if (absDeltaX > ViewConfiguration.get(getContext()).getScaledTouchSlop() ||
                    absDeltaY > ViewConfiguration.get(getContext()).getScaledTouchSlop()) {
                    getParent().requestDisallowInterceptTouchEvent(false);
                }*/
            case TouchEvent.PRIMARY_POINT_UP:
                break;
            default:
                break;
        }
        return super.isTouchFocusable();
    }
}
